package com.unitd.frame.spring.helper;

import org.springframework.aop.framework.AdvisedSupport;
import org.springframework.aop.framework.AopProxy;
import org.springframework.aop.support.AopUtils;
import org.springframework.util.ClassUtils;

import java.lang.reflect.Field;

/**
 * @desc SpringAOP工具类,用于处理代理对象信息
 * @filename EnvironmentHelper.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date 2016/10/14
 */
public class SpringAopHelper {

	/**
	 * @desc 获取 目标对象
	 * @param proxy 代理对象
	 * @return Object
	 */
	public static Object getTarget(Object proxy) throws Exception {

		// 不是代理对象
		if (!AopUtils.isAopProxy(proxy)) {
			return proxy;
		}

		if (AopUtils.isJdkDynamicProxy(proxy)) {
			Object object = getJdkDynamicProxyTargetObject(proxy);
			return getTarget(object);
		} else { //cglib
			Object object = getCglibProxyTargetObject(proxy);
			return getTarget(object);
		}
	}

	public Class<?> getTargetClass(Object object) {
		return ClassUtils.getUserClass(object);
	}

	private static Object getCglibProxyTargetObject(Object proxy) throws Exception {
		Field h = proxy.getClass().getDeclaredField("CGLIB$CALLBACK_0");
		h.setAccessible(true);
		Object dynamicAdvisedInterceptor = h.get(proxy);

		Field advised = dynamicAdvisedInterceptor.getClass().getDeclaredField("advised");
		advised.setAccessible(true);

		return ((AdvisedSupport) advised.get(dynamicAdvisedInterceptor)).getTargetSource().getTarget();
	}

	/**
	 * @desc 获取动态代理对象
	 * @param proxy 代理对象
	 * @return Object
	 */
	private static Object getJdkDynamicProxyTargetObject(Object proxy) throws Exception {
		Field h = proxy.getClass().getSuperclass().getDeclaredField("h");
		h.setAccessible(true);
		AopProxy aopProxy = (AopProxy) h.get(proxy);

		Field advised = aopProxy.getClass().getDeclaredField("advised");
		advised.setAccessible(true);

		return ((AdvisedSupport) advised.get(aopProxy)).getTargetSource().getTarget();
	}
}  